package com.kmzx.one.project.config.listener;

import com.kmzx.one.common.constants.Constants;
import com.kmzx.one.project.events.InvalidOrderEvent;
import com.kmzx.one.project.events.UpdateSkuEvent;
import com.kmzx.one.project.events.UserRewardEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;


@Component
public class JRedisMessageListener implements MessageListener {

    public final static String LISTENER_PATTERN = "__key*__:*";

    private static Logger logger = LoggerFactory.getLogger(JRedisMessageListener.class);


    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();// 建议使用: valueSerializer

        byte[] channel = message.getChannel();

        logger.info("onMessage >> ");

        logger.info("channel: {}, body: {}, bytes: {}", new String(channel), new String(body), new String(pattern));

        String combineOrder = new String(body);

        String prefix = combineOrder.substring(0, 2);

        System.out.println("prefix = " + prefix);
        System.out.println("combineOrder = " + combineOrder);

        if (prefix != null && Constants.rewardOrderPrefix.equals(prefix)) {
            String endInfo = combineOrder.substring(2);

            String[] threeInfo = endInfo.split(",");
            String moneyStr = threeInfo[1];
            BigDecimal billMoney = new BigDecimal(moneyStr);
            Long timeStamp = Long.valueOf(threeInfo[3]);

            UserRewardEvent userRewardEvent = new UserRewardEvent(threeInfo[0], billMoney, threeInfo[2]);
            userRewardEvent.setTimeStamp(timeStamp);
            if (threeInfo.length == 5) {
                String[] s = threeInfo[0].split("_");
                userRewardEvent.setUserId(s[0]);
                userRewardEvent.setProfit(1);
                userRewardEvent.setInOrOut(Integer.valueOf(s[1]));

            } else {
                String[] s = threeInfo[0].split("_");
                userRewardEvent.setInOrOut(Integer.valueOf(s[1]));
                userRewardEvent.setUserId(s[0]);
            }
            applicationContext.publishEvent(userRewardEvent);
        }

        if (prefix != null && Constants.lifestyleOrderPrefix.equals(prefix)) {
            String endInfo = combineOrder.substring(2);
            InvalidOrderEvent event = new InvalidOrderEvent(endInfo);
            applicationContext.publishEvent(event);

        }


        String warehouse = new String(channel);

        if (warehouse != null && "2)updateWarehouse".equals(warehouse)) {
            String skuListOld = new String(body);
            UpdateSkuEvent event = new UpdateSkuEvent(skuListOld);
            applicationContext.publishEvent(event);
        }
    }
}
